﻿using System;

// XNA lib
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;



//Oak Engine Lib
using OakEngine.GameState;
using OakEngine.Input;
using Microsoft.Xna.Framework.Input;

namespace MainJumpFleet.GameStates
{

    public struct MotherShip
    {
        public JumpFleetGameLib.xSprite.StaticSprite sprite;
        public float rotationSpeed;
        public float movementSpeed;
        public Vector2 forwardVectorNormalized;
        public Vector2 waypoint1;
        public Vector2 waypoint2;
        public Vector2 waypoint3;
    }

    public struct SatelliteShip
    {
        public JumpFleetGameLib.xSprite.StaticSprite sprite;                
    }

   public class FleetRotationGameScreenState : OAK_IGameState
    {
        #region OAK_IGameState Members
        SpriteBatch batch;
        SpriteFont mainMenuFont;
                      
        // holds the SpriteManager object
        private JumpFleetGameLib.xSprite.SpriteManager spriteManager;

        MotherShip motherShip;
        SatelliteShip blueShip;
        SatelliteShip greenShip;
        SatelliteShip redShip;

        SpriteFont debugFont;
        Vector2 leftStickPosition;
        
        public FleetRotationGameScreenState()
        {
            Enter();
        }


        public void Enter()
        {
            mainMenuFont = CGame.CGame.GetInstance().Content.Load<SpriteFont>("Fonts\\mainMenuFont");
            batch = CGame.CGame.GetInstance().SpriteBatch;
            
            // loads the debug font
            debugFont = CGame.CGame.GetInstance().Content.Load<SpriteFont>(@"Fonts\mainMenuFont");

            // creates the SpriteManager object
            spriteManager = new JumpFleetGameLib.xSprite.SpriteManager();
            
            // creates the sprite templates            
            spriteManager.CreateStaticSpriteTemplate("MotherShip", CGame.CGame.GetInstance().Content.Load<Texture2D>(@"textures\ship2_77x128"), 0, 0, 77, 128, Color.White, 255, 0, JumpFleetGameLib.xSprite.RotationAnchorPoint.Center, 1.0f, SpriteEffects.None, 0.300f);
            spriteManager.CreateStaticSpriteTemplate("BlueShip", CGame.CGame.GetInstance().Content.Load<Texture2D>(@"textures\ship1_76x128"), 0, 0, 77, 128, Color.Blue, 255, 0, JumpFleetGameLib.xSprite.RotationAnchorPoint.Center, 0.5f, SpriteEffects.None, 0.301f);
            spriteManager.CreateStaticSpriteTemplate("GreenShip", CGame.CGame.GetInstance().Content.Load<Texture2D>(@"textures\ship1_76x128"), 0, 0, 77, 128, Color.Green, 255, 0, JumpFleetGameLib.xSprite.RotationAnchorPoint.Center, 0.5f, SpriteEffects.None, 0.301f);
            spriteManager.CreateStaticSpriteTemplate("RedShip", CGame.CGame.GetInstance().Content.Load<Texture2D>(@"textures\ship1_76x128"), 0, 0, 77, 128, Color.Red, 255, 0, JumpFleetGameLib.xSprite.RotationAnchorPoint.Center, 0.5f, SpriteEffects.None, 0.301f);

            // sets up the mother ship's members
            motherShip = new MotherShip();
            motherShip.sprite = spriteManager.CreateNewStaticSpriteFromTemplate("MotherShip");
            motherShip.sprite.Visible = true;
            motherShip.sprite.DrawPosition = new Vector2(CGame.CGame.GetInstance().Device.Viewport.Width / 2, CGame.CGame.GetInstance().Device.Viewport.Height / 2);
            motherShip.sprite.RotationAmount = 0;
            motherShip.rotationSpeed = 1.0f;
            motherShip.movementSpeed = 50.0f;
            motherShip.forwardVectorNormalized = new Vector2(0, -1);
            motherShip.forwardVectorNormalized.Normalize();
            motherShip.waypoint1 = motherShip.sprite.DrawPosition + new Vector2(-100.0f, 50.0f);
            motherShip.waypoint2 = motherShip.sprite.DrawPosition + new Vector2(0, 128.0f);
            motherShip.waypoint3 = motherShip.sprite.DrawPosition + new Vector2(100.0f, 50.0f);

            // sets up the blue ship's members
            blueShip = new SatelliteShip();
            blueShip.sprite = spriteManager.CreateNewStaticSpriteFromTemplate("BlueShip");
            blueShip.sprite.Visible = true;
            blueShip.sprite.DrawPosition = motherShip.waypoint1;
            blueShip.sprite.RotationAmount = 0;

            // sets up the green ship's members
            greenShip = new SatelliteShip();
            greenShip.sprite = spriteManager.CreateNewStaticSpriteFromTemplate("GreenShip");
            greenShip.sprite.Visible = true;
            greenShip.sprite.DrawPosition = motherShip.waypoint2;
            greenShip.sprite.RotationAmount = 0;

            // sets up the red ship's members
            redShip = new SatelliteShip();
            redShip.sprite = spriteManager.CreateNewStaticSpriteFromTemplate("RedShip");
            redShip.sprite.Visible = true;
            redShip.sprite.DrawPosition = motherShip.waypoint3;
            redShip.sprite.RotationAmount = 0;
        }

        public bool Input(OAK_InputManager input)
        {
            if (input.Pressed(Keys.Escape) || input.Pressed(Buttons.Back, 0))
                CGame.CGame.GetInstance().ChangeState(new MainMenuState());

            leftStickPosition = input.LeftStickPosition(0);

            return false;
        }

        public void Update(GameTime dt)
        {
            if (leftStickPosition.X != 0)
            {
                float rotationAmount = motherShip.rotationSpeed * leftStickPosition.X * (float)dt.ElapsedGameTime.TotalSeconds;

                // rotate the mother ship sprite
                motherShip.sprite.RotationAmount += rotationAmount;

                // calculate the mother ship forward vector
                motherShip.forwardVectorNormalized = JumpFleetGameLib.VectorTransformations.RotateVector(motherShip.sprite.RotationAmount);

                // calculate the mother ship's waypoint rotation
                motherShip.waypoint1 = JumpFleetGameLib.VectorTransformations.RotateVectorAroundParentObject(motherShip.sprite.DrawPosition, motherShip.waypoint1, rotationAmount);
                motherShip.waypoint2 = JumpFleetGameLib.VectorTransformations.RotateVectorAroundParentObject(motherShip.sprite.DrawPosition, motherShip.waypoint2, rotationAmount);
                motherShip.waypoint3 = JumpFleetGameLib.VectorTransformations.RotateVectorAroundParentObject(motherShip.sprite.DrawPosition, motherShip.waypoint3, rotationAmount);

                // rotate the satellite ship sprites
                blueShip.sprite.RotationAmount += rotationAmount;
                greenShip.sprite.RotationAmount += rotationAmount;
                redShip.sprite.RotationAmount += rotationAmount;

                // snap the satellite ships to the waypoints
                blueShip.sprite.DrawPosition = motherShip.waypoint1;
                greenShip.sprite.DrawPosition = motherShip.waypoint2;
                redShip.sprite.DrawPosition = motherShip.waypoint3;

            }

            if (leftStickPosition.Y < 0)
            {
                // calculates the total distance to move the mother ship
                float movementDistance = motherShip.movementSpeed * Math.Abs(leftStickPosition.Y) * (float)dt.ElapsedGameTime.TotalSeconds;

                // holds the current relative positions of the waypoint
                Vector2 currentDistanceToWaypoint1 = motherShip.waypoint1 - motherShip.sprite.DrawPosition;
                Vector2 currentDistanceToWaypoint2 = motherShip.waypoint2 - motherShip.sprite.DrawPosition;
                Vector2 currentDistanceToWaypoint3 = motherShip.waypoint3 - motherShip.sprite.DrawPosition;

                // applies the distance to the mother ship's forward vector
                motherShip.sprite.DrawPosition += new Vector2(motherShip.forwardVectorNormalized.X * movementDistance, motherShip.forwardVectorNormalized.Y * movementDistance);

                // repositions the waypoints after the mother ship moves
                motherShip.waypoint1 = motherShip.sprite.DrawPosition + currentDistanceToWaypoint1;
                motherShip.waypoint2 = motherShip.sprite.DrawPosition + currentDistanceToWaypoint2;
                motherShip.waypoint3 = motherShip.sprite.DrawPosition + currentDistanceToWaypoint3;

                // snaps the satellite ships to the waypoints
                blueShip.sprite.DrawPosition = motherShip.waypoint1;
                greenShip.sprite.DrawPosition = motherShip.waypoint2;
                redShip.sprite.DrawPosition = motherShip.waypoint3;
            }

             
                        
        }

        public void Render()
        {
            // batch.DrawString(mainMenuFont, "Welcome to Camera Game Screen State\n Press Esc to go back to Main menu ", new Vector2(300,250), Color.White);

            // ends the SpriteBatch from CGame
            batch.End();

            CGame.CGame.GetInstance().Device.Clear(Color.Black);            

            // starts the local SpriteBatch
            batch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);

            // let the SpriteManager do it's job            
            spriteManager.Draw(batch);

            // debug info
            batch.DrawString(debugFont, "Left Stick Position:" + leftStickPosition.ToString() , new Vector2(10, 10), Color.White);
            batch.DrawString(debugFont, "Forward Vector:" + motherShip.forwardVectorNormalized.ToString(), new Vector2(10, 60), Color.White);

            // ends the local SpriteBatch
            batch.End();
      
            // starts the SpriteBatch again so I don't break the other GameStates
            batch.Begin();

        }

        public void Exit()
        {
        }

        #endregion
    }
}
